<template>
  <div class="pagebox app-container">
    <a-row :gutter="16">
      <a-col class="gutter-row" :span="12">
        <div class="gutter-box bg">
          <div class="topoptions">
            <span class="colorin">机床总数</span>
            <a-select  :getPopupContainer="(triggerNode) => triggerNode.parentNode" :default-value="1" style="width: 120px" @change="handleArchiveNumChange">
              <a-select-option :value="item.value" v-for="(item,index) in numlistone" :key="index">
                {{ item.name }}
              </a-select-option>

            </a-select>
          </div>
          <barChartSort v-if="chartDataBarsort" :chart-data="chartDataBarsort" />
          
          <div class="btnbox">
              <ExportFile  :fetch="getDownloadNumData" :fields="jsonFields"  name="机床数量.xls">
            <a-button type="primary" :loading="iconLoading">
              导出
            </a-button>
            </ExportFile>
          </div>

        </div>
      </a-col>
      <a-col class="gutter-row" :span="12">
        <div class="gutter-box bg">
          <div class="topoptions">
            <span class="colorin">在线数量</span>
            <a-select  :getPopupContainer="(triggerNode) => triggerNode.parentNode" :default-value="1" style="width: 120px" @change="handleChange">
              <a-select-option :value="item.value" v-for="(item,index) in numlisttwo" :key="index">
                {{ item.name }}
              </a-select-option>

            </a-select>
          </div>
          <barChartSort :chart-data="chartDataBarsort2" :className="'chartDataBarsort2'" />
          <div class="btnbox">
               <ExportFile  :fetch="getDownloadrunData" :fields="jsonnumFields"  name="机床在线数量.xls">
            <a-button type="primary" :loading="iconLoading">
              导出
            </a-button>
              </ExportFile>
          </div>

        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script>
import barChartSort from "@/pages/echartsCom/barChartSort";
import api from "@/api/statisticalForm.js";

export default {
  name: "index",
  components: {
    barChartSort
  },
  data() {
    return {
      chartDataBarsort: {
        expectedData: [],
        data: [],
        color: "#2297F0"
      },
      chartDataBarsort2: {
        expectedData: [],
        data: [],
        color: "#F9C83F"
      },
      // numList: [{ name: "1-10", value: 1 }, { name: "11-20", value: 2 }, { name: "21-30", value: 3 }, {
      //   name: "31-40",
      //   value: 4
      // }, { name: "21-30", value: 5 }, { name: "41-50", value: 6 }, { name: "51-60", value: 7 }, {
      //   name: "61-70",
      //   value: 8
      // }, { name: "71-80", value: 9 }, { name: "81-90", value: 10 }],
      params: {
        page: 1,//页码
        pageSize: 10
      },
      ArchiveNumpage: 1,
      page: 1,
      iconLoading:false,
      jsonFields:{'公司名称':'companyName',"机床数量":"sum"},
      jsonnumFields:{'公司名称':'companyName',"在线数量":"runSum"},
      ArchiveNumtotal:0,
      ArchiveRuntotal:0,
      numlistone:[],
      numlisttwo:[]
    };
  },
  created() {
    this.params.username = localStorage.getItem("userName");
    this.getArchiveNumByUsername();
    this.getArchiveRunNumByUsername();
  },
  methods: {
     numLISTsum(num){
        let list=[]
        for(var i=1;i<=num; i++){
          list.push({name:`${(10*(i-1))+1}-${(10*(i-1))+10}`,value:i})
        }
        return list
    },
    //总数
    getArchiveNumByUsername() {
      this.params.page = this.ArchiveNumpage;
      api.getArchiveNumByUsername(this.params).then(res => {
        let rows = res.Data.rows;
       
        this.ArchiveNumtotal=res.Data.total;
         let num=Math.ceil( this.ArchiveNumtotal/10);
          this.numlistone=this.numLISTsum(num);
        let expectedData = [];
        let data = [];
        rows.forEach(item => {
          expectedData.push(item.sum);
          data.push(item.companyName);
        });
        this.chartDataBarsort.expectedData = expectedData;
        this.chartDataBarsort.data = data;
      });
    },
    //在线数量
    getArchiveRunNumByUsername() {
      this.params.page = this.page;
      api.getArchiveRunNumByUsername(this.params).then(res => {
         let rows = res.Data.rows;
         this.ArchiveRuntotal=res.Data.total;
          let num=Math.ceil( this.ArchiveRuntotal/10);
           this.numlisttwo=this.numLISTsum(num);
        let expectedData = [];
        let data = [];
        rows.forEach(item => {
          expectedData.push(item.runSum);
          data.push(item.companyName);
        });
        this.chartDataBarsort2.expectedData = expectedData;
        this.chartDataBarsort2.data = data;
      });
    },
    handleArchiveNumChange(value) {
      this.ArchiveNumpage = value;
      this.getArchiveNumByUsername();
    },
    handleChange(value) {
      this.page = value;
      this.getArchiveRunNumByUsername();
    },
     // 导出
    async getDownloadNumData() {
      let _this = this;
      if (_this.iconLoading == true) {
        return false;
      }
      let total = _this.ArchiveNumtotal;
      let pageSize = 300;
      let length = Math.ceil(total / pageSize);
      let newLists = [];
      _this.iconLoading = true;
      for (let i = 1; i <= length; i++) {
        let lists = await this.buildDownloadLists({
          page: i,
          pageSize: pageSize
        });
        newLists.push(lists);
      }
      _this.iconLoading = false;
      return [].concat(...newLists);
    },
    async buildDownloadLists(parames) {
      let _this = this;
      let { page, pageSize } = parames;
      _this.params.pageSize=pageSize,
       _this.params.page=page
      const res = await api.getArchiveNumByUsername({
      ..._this.params
       
      });
      if (res.MsgCode !== 0) {
        return;
      }
      return res.Data.rows;
    },

    //导出运行时间
async getDownloadrunData() {
      let _this = this;
      if (_this.iconLoading == true) {
        return false;
      }
      let total = _this.ArchiveRuntotal;
      let pageSize = 300;
      let length = Math.ceil(total / pageSize);
      let newLists = [];
      _this.iconLoading = true;
      for (let i = 1; i <= length; i++) {
        let lists = await this.buildDownloadListsrun({
          page: i,
          pageSize: pageSize
        });
        newLists.push(lists);
      }
      _this.iconLoading = false;
      return [].concat(...newLists);
    },
    async buildDownloadListsrun(parames) {
      let _this = this;
      let { page, pageSize } = parames;
      _this.params.pageSize=pageSize,
       _this.params.page=page
      const res = await api.getArchiveRunNumByUsername({
      ..._this.params
       
      });
      if (res.MsgCode !== 0) {
        return;
      }
      return res.Data.rows;
    },

  }
};
</script>

<style scoped lang="less">
@import "index";
</style>
